package com.youtellv1.service.Impl;

import com.youtellv1.entity.ChildSign;
import com.youtellv1.mapper.ChildSignMapper;
import com.youtellv1.service.ChildSignService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ChildSignServiceImpl implements ChildSignService {
    @Resource
    private ChildSignMapper childSignMapper;

    @Override
    public List<ChildSign> findAllService() {
        return childSignMapper.findAllChildSign();
    }

    @Override
    public ChildSign findByCidService(Integer cid) {
        return childSignMapper.findByCid(cid);
    }

    @Override
    public int countByCidAndDateRange(Integer cid, String start, String end) {
        return childSignMapper.countByCidAndDateRange(cid, start, end);
    }

    @Override
    public void saveService(ChildSign childSign) {
        childSignMapper.save(childSign);
    }

    @Override
    public void updateService(ChildSign childSign) {
        childSignMapper.update(childSign);
    }

    @Override
    public void deleteService(ChildSign childSign) {
        childSignMapper.delete(childSign);
    }

    // 判断今天是否签到
    @Override
    public boolean isTodaySigned(Integer cid) {
        Date today = new Date(); // 当前日期
        return childSignMapper.countByCidAndDate(cid, today) > 0;
    }

    @Override
    public void insertTodaySign(Integer cid) {
        ChildSign sign = new ChildSign();
        sign.setCid(cid);
        sign.setSdate(new Date());
        childSignMapper.insert(sign);
    }

    @Override
    public boolean hasSignedToday(Integer cid) {
        return childSignMapper.countByCidAndDate(cid, new Date()) > 0;
    }

    // 获取全部签到日期
    @Override
    public List<Date> findAllSignDates(Integer cid) {
        return childSignMapper.selectAllDatesByCid(cid);
    }

    // 获取本月签到日期
    @Override
    public List<Date> findThisMonthSignDates(Integer cid) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date start = cal.getTime(); // 月初
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        Date end = cal.getTime(); // 月末
        return childSignMapper.selectDatesByCidBetween(cid, start, end);
    }

    @Override
    public boolean hasSignedOnDate(Integer cid, Date sdate) {
        return childSignMapper.countByCidDate(cid, sdate) > 0;
    }

    @Override
    public void insertSignRecord(Integer cid, Date sdate) {
        childSignMapper.insertChildSign(cid, sdate);
    }

}
